package cn.vic.genertor.other;

import java.util.LinkedList;


/**
 * Created by vic on 2017/11/16.
 */
public class PrintList extends LinkedList<JsonValue> {

    private static int maxIndex = 0;

    /**
     * 打印信息
     */
    public String print(int headLength) {
        StringBuffer printBuffer = new StringBuffer();
        for (JsonValue json : this) {
            StringBuffer tempBuffer = new StringBuffer();
            for (int i = 1; i < json.getIndex(); i++) {
                tempBuffer.append("|  ");
            }
            if (json.getValue() != null) {
                //|字段|类型|说明
                tempBuffer.append(String.format("|%s|%s|", json.getKey(), json.getValue().getClass().getSimpleName()));
            } else {
                tempBuffer.append(String.format("|%s|%s|", json.getKey(), " "));
            }
            int length = tempBuffer.toString().split("\\|").length - 1;
            for (int i = length; i < headLength; i++) {
                tempBuffer.append("  |");
            }
            tempBuffer.append("\n");
            printBuffer.append(tempBuffer);
        }
        return printBuffer.toString();
    }

    public String print2(int headLength) {
        StringBuffer printBuffer = new StringBuffer();
        for (JsonValue json : this) {
            StringBuffer tempBuffer = new StringBuffer();
            //前“|” 填充
            tempBuffer.append(poleOut(json.getIndex() - 1));
            //字段
            tempBuffer.append(String.format("|%s|", json.getKey()));
            //这个值到时候可以从haed中判断取
            int typeIndex = 2;
            //中部竖杆填充
            int middle = headLength - json.getIndex() - typeIndex;
            tempBuffer.append(poleOut(middle));
            //字段类型
            if (json.getValue() != null) {
                tempBuffer.append(String.format("%s|", json.getValue().getClass().getSimpleName()));
            } else {
                tempBuffer.append(String.format("%s|", " "));
            }
            //后部竖杆填充
            tempBuffer.append(poleOut(typeIndex - 1));
            tempBuffer.append("\n");
            printBuffer.append(tempBuffer);
        }
        return printBuffer.toString();
    }

    /**
     * 竖杆填充输出
     *
     * @param widthValue
     * @return
     */
    private StringBuffer poleOut(int widthValue) {
        StringBuffer space = new StringBuffer();
        for (int i = 0; i < widthValue; i++) {
            space.append(" | ");
        }
        return space;
    }

    /**
     * 获取打印信息
     *
     * @return
     */
    public String getAll() {
        String head = printHead();
        StringBuffer buffer = new StringBuffer(head);
        //
        int length = maxIndex + 2;
        String context = print2(length);
        buffer.append(context);
        return buffer.toString();
    }

    /**
     * 打印头部
     */
    public String printHead() {
        String[] numStr = {"〇", "一", "二", "三", "四", "五", "六", "七", "九", "十"};
        StringBuffer buffer = new StringBuffer("|");
        for (int i = 1; i <= maxIndex; i++) {
            buffer.append(numStr[i]).append("级字段|");
        }
        buffer.append("类型|说明|").append("\n");

        for (int i = 1; i <= maxIndex+2; i++) {
            buffer.append("|:--");
        }
        buffer.append("\n");
        return buffer.toString();
    }

    /**
     * 添加json value 信息
     *
     * @param index
     * @param key
     * @param value
     */
    public void addPrint(int index, Object key, Object value) {
        JsonValue jsonValue = new JsonValue(index, key, value);
        if (maxIndex < index) {
            maxIndex = index;
        }
        this.add(jsonValue);
    }
}
